﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AliboboStockLib.linq;
using AliboboStockLib.util;

namespace AliboboStockLib.stockupdater.taskObj
{
    public class TaskManager
    {
        public List<ObjectId> getTasks()
        {
            MyStockDataContext context = new MyStockDataContext();
            List<ObjectId> r = (from t in context.ObjectIds orderby t.objectType, t.version select t).ToList<ObjectId>();
            return r;
        }


        public void createTask(String name, String Type, ref Guid id)
        {

            if (getTaskId(name, Type, ref id))
                return;
            MyStockDataContext context = LinqAgent.getContext();
            ObjectId o = new ObjectId();
            o.id = NewGUID.create("obid");
            o.objectName = name;
            o.objectType = Type;
            o.version = "1.0";
            o.actived = true;
            context.ObjectIds.InsertOnSubmit(o);
            context.SubmitChanges();
            id = o.id.Value;

        }
        public bool getTaskId(String name, String Type, ref Guid id)
        {
            MyStockDataContext context = LinqAgent.getContext();
            ObjectId o = (from t in context.ObjectIds where t.objectName.Equals(name) select t).FirstOrDefault<ObjectId>();
            if (o != null)
            {
                id = o.id.Value;
                return true;
            }
            return false;
        }
    }
}
